const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function transferMarchPos(lng, lat) {
  // var x_pi = Math.PI * 3000.0 / 180.0;
  // var x = lng, y = lat;
  // var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  // var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  // var bd_lon = z * Math.cos(theta) + 0.0065;
  // var bd_lat = z * Math.sin(theta) + 0.006;
  // return [bd_lat, bd_lon];
  var x_pi = Math.PI * 3000.0 / 180.0;
  var x = lng - 0.0065, y = lat - 0.006;
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  var gg_lon = z * Math.cos(theta);
  var gg_lat = z * Math.sin(theta);
  return [gg_lat, gg_lon];
}

function isPhoneAvailable(phonenum) {
  var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
  if (!myreg.test(phonenum)) {
    return false;
  } else {
    return true;
  }
}

module.exports = {
  formatTime: formatTime,
  transferMarchPos: transferMarchPos,
  checkPhoneNumber:isPhoneAvailable
}
